﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Entity.YiDongYuan;
using YiSha.Model.Param.YiDongYuan;
using YiSha.Service.YiDongYuan;

namespace YiSha.Business.YiDongYuan
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2021-06-09 22:45
    /// 描 述：尾气检测结果业务类
    /// </summary>
    public class DetectionResultBLL
    {
        private DetectionResultService detectionResultService = new DetectionResultService();

        #region 获取数据

        public async Task<TData<List<DetectionResultEntity>>> GetList(DetectionResultListParam param)
        {
            TData<List<DetectionResultEntity>> obj = new TData<List<DetectionResultEntity>>();
            obj.Data = await detectionResultService.GetList(param);
            obj.Total = obj.Data.Count;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<List<DetectionResultEntity>>> GetPageList(DetectionResultListParam param, Pagination pagination)
        {
            TData<List<DetectionResultEntity>> obj = new TData<List<DetectionResultEntity>>();
            obj.Data = await detectionResultService.GetPageList(param, pagination);
            obj.Total = pagination.TotalCount;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<DetectionResultEntity>> GetEntity(long id)
        {
            TData<DetectionResultEntity> obj = new TData<DetectionResultEntity>();
            obj.Data = await detectionResultService.GetEntity(id);
            if (obj.Data != null)
            {
                obj.Tag = 1;
            }
            return obj;
        }

        public async Task<TData<DetectionResultEntity>> GetLastResult(long userId, string code)
        {
            TData<DetectionResultEntity> obj = new TData<DetectionResultEntity>();
            obj.Data = await detectionResultService.GetLastResult(userId, code);
            if (obj.Data != null)
            {
                obj.Tag = 1;
            }
            return obj;
        }

        #endregion 获取数据

        #region 提交数据

        public async Task<TData<string>> SaveForm(DetectionResultEntity entity)
        {
            //根据最大K值和限值，计算超标值(差)，计算超标百分比（%）
            entity.createTime = DateTime.Now;
            double[] kValue = new double[]{
                    entity.k1,
                    entity.k2,
                    entity.k3,
                };
            entity.accmax = kValue.Max();
            entity.accavg = kValue.Average();
            entity.accdiff = entity.accavg - entity.acclimits;
            entity.testresult = entity.accdiff <= 0 ? "0" : "1";
            TData<string> obj = new TData<string>();
            await detectionResultService.SaveForm(entity);
            obj.Data = entity.Id.ParseToString();
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData> DeleteForm(string ids)
        {
            TData obj = new TData();
            await detectionResultService.DeleteForm(ids);
            obj.Tag = 1;
            return obj;
        }

        #endregion 提交数据
    }
}
